/**
 * kaGridData 封装
 * 
 * 版本：v1.0
 * 
 * Stanley
 **/
layui.define(['jquery'],
    function (exports) {
        var $ = layui.jquery;
        var kaGridData = new function () {
            this.get = function (params, dataAllBoolean, origOptions, origOptionstype, origOptionsDataAllBoolean) {
                var kaGridAllDataName = "table_Data_" + params.id;
                // 判断是非更新
                if (params.dataBoolean == 0) { // 单纯获取本地
                    return layui.kaGridData.getFilter(top.kaGridAllData[kaGridAllDataName], dataAllBoolean, params.id);
                } if (params.dataBoolean == 3) { // 单纯获取本地
                    // 初始化
                    if (top.kaGridAllData == undefined || top.kaGridAllData == null) { top.kaGridAllData = {}; }
                        top.kaGridAllData[kaGridAllDataName] = params.data;
                    return layui.kaGridData.getFilter(params.data, dataAllBoolean, params.id);
                } else {
                    // 加载后端数据
                    if (params.dataBoolean == 1) { // 初始化
                        // 请求后端数据
                        if (origOptionsDataAllBoolean == 0) {
                            var ajaxData = {
                                code:  200,
                                data: layui.kaGridData.getFilter(top.kaGridAllData[kaGridAllDataName], dataAllBoolean, params.id),
                                message: "获取数据成功",
                                msg: "操作成功"
                            }
                            return origOptions.success(ajaxData);;
                        } else {
                            var ajaxOptionStr = "1";
                            var urlMethod = origOptions.type.toLowerCase();
                             var dataStr = JSON.parse(origOptions.data);
                            if(urlMethod == "get") {
                                var symbol = origOptions.url.indexOf('?') !== -1 ? '&' : '?';
                                var mb = "";
                                for (var key in dataStr) {
                                    if (mb == "") { 
                                        mb = key + "=" + dataStr[key];
                                    } else {
                                        mb = mb + "&" + key + "=" + dataStr[key];
                                    }
                                }
                                if(!(symbol == "?"  && mb == "")){
                                    origOptions.url = origOptions.url + symbol + mb;
                                }
                                ajaxOptionStr = "";
                            } else {
                                origOptions.data = JSON.stringify(dataStr);
                            }
                            var ajaxOption = {
                                kaGridAllDataName: kaGridAllDataName,
                                dataAllBoolean: dataAllBoolean,
                                paramsId: params.id,
                                type: origOptions.type,
                                cache: false,
                                async: origOptions.async, //同步 false
                                url: origOptions.url,
                                contentType: origOptions.contentType,
                                dataType: origOptions.dataType,
                                params: params,
                                beforeSend: function (xhr) {
                                    var paramsx = this.params;
                                    if (paramsx.headerBoolean == undefined || paramsx.headerBoolean || paramsx.headerBoolean == "true") {
                                        xhr.setRequestHeader("Authorization", 'Bearer ' + layui.kaToken.get());
                                    }
                                }
                            };
                            if(ajaxOptionStr != "") {
                                ajaxOption.data = origOptions.data;
                            }
                            $.ajax(ajaxOption)
                            .done(function (ajaxData) {
                                var xparams = top.kaGridAllParams["table_Params_" + this.paramsId];
                                // 调用原始的 success 回调
                                // 赋值
                                xparams.data = ajaxData.data;
                                // 初始化
                                if (top.kaGridAllData == undefined || top.kaGridAllData == null) { top.kaGridAllData = {}; }
                                    top.kaGridAllData[kaGridAllDataName] = xparams.data;
                                // 返回
                                ajaxData.data = layui.kaGridData.getFilter(xparams.data, this.dataAllBoolean, xparams.id);
                                origOptions.success(ajaxData);
                            })
                            .fail(function (xhr, status, error) {
                                // 调用原始的 error 回调
                                origOptions.error(xhr, status, error);
                            })
                            .always(function () {
                                // 调用原始的 complete 回调
                                if (typeof origOptions.complete === "function") {
                                    origOptions.complete(); 
                                }
                            });
                        }
                         
                    } else if (params.dataBoolean == 2) { // 更新
                        params.dataBoolean = 1;
                        params.data = layui.kaGridData.getFilter(this.get(params), dataAllBoolean, params.id);
                        // 更新表格
                        layui.kaGrid.render(params);
                    }
                }
            } 
            // 获取数据隔离
            this.getFilter = function (datas, dataAllBoolean, tableid) {
                var kaGridAllParamsName = "table_Params_" + tableid;
                if (dataAllBoolean != undefined) {
                    return datas;
                } else {
                    var params = top.kaGridAllParams[kaGridAllParamsName];// 获取表格字段
                    var sunMap = new Map();
                    var newData = [];
                    // tableData
                    var numberId = 0;
                    for (var i in datas) {
                        var data = datas[i];
                        var filter = data["kaGridChooseFilter"];
                        var boolean = false;
                        for (var key in filter) {
                            numberId++;
                            boolean = true;
                            var vBoolean = filter[key];
                            if (!vBoolean) {
                                boolean = false;
                                sunMap.set(key, key);
                                break;
                            }
                        }
                        if (boolean) {
                            newData.push(data);
                        }
                    }
                    if (numberId == 0) {
                        return datas;
                    } else {
                        params.filtersTwo = sunMap;
                        return newData;
                    }
                }
            }
        };
        exports('kaGridData', kaGridData);
    }
)